
#include <cstdio>
#include <BKeyEvent>
#include <BMouseEvent>
#include <BCharEvent>
#include <BDragEvent>
#include <BTextEdit>
#include <BPainter>
#include <BCursor>
#include <BDrag>
#include <BSystem>
#include <BScreen>

#include "IWindow.h"
#include "member_BWidget.h"
#include "member_BEvent.h"

using namespace BWE;

extern double app_clickInterval;
extern BList<IWindowHolder> app_windows;

IWindow::IWindow(BWidget* widget)
{
	boss = widget;
	shown = false;
	drag = 0;
	event_member(&event)->window = this;

	focusing = false;
	hoverWidget = 0;
	focusWidget = 0;
	
	mouse_button = Button_None;
	mouse_pressed = false;
	mouse_click_time = 0;
	keyStates.resize(Key_Max);

	tipsShown = false;
	tipsTick = 0;

	refresh = false;

	if (create())
	{
		app_windows.append(this);
	}
}
IWindow::~IWindow()
{

}

void IWindow::update()
{
	event.reset(Event_Update);
	updateWidget(boss, event);
	if (hoverWidget && tipsTick > 0 && !tipsShown)
	{
		clock_t now = clock();
		if (now - tipsTick > 1000)
		{
			tipsShown = true;
			refresh = true;
		}
	}
}
void IWindow::fresh()
{
	event.reset(Event_Fresh);
	freshWidget(boss, event);
}
void IWindow::frame()
{
	if (refresh)
	{
		makeCurrent(true);
		BOpenGL::Init();
		{
			frameWidget(boss);

			glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
			glClear(GL_COLOR_BUFFER_BIT);

			glDisable(GL_LIGHTING);
			glEnable(GL_CULL_FACE);
			glDisable(GL_DEPTH_TEST);

			glEnable(GL_BLEND);
			glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE);
			glBlendEquationSeparate(GL_FUNC_ADD, GL_FUNC_ADD);

			glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
			glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);
			glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST);

			glDisable(GL_POLYGON_OFFSET_POINT);
			glDisable(GL_POLYGON_OFFSET_LINE);
			glDisable(GL_POLYGON_OFFSET_FILL);

			drawWindow();
			drawCursor();
			drawDrag();
			drawTips();

			swapBuffers();
		}
		BOpenGL::Release();
		makeCurrent(false);
	}
	refresh = false;
}

void IWindow::updateWidget(BWidget* widget, const BEvent& event)
{
	if (widget && widget_member(widget)->state.enabled)
	{
		widget_member(widget)->procEvent(event);
		for (int i = 0; i < widget->childCount(); i++)
		{
			BWidget* child = widget->child(i);
			updateWidget(child, event);
		}
	}
}
void IWindow::freshWidget(BWidget* widget, const BEvent& event)
{
	if (widget == 0 || !widget_member(widget)->visible)
		return;
	if (!widget_member(widget)->ignored())
		return;
	if (widget_member(widget)->refresh)
	{
		widget_member(widget)->refresh = false;
		widget_member(widget)->procEvent(event);
		widget_member(widget)->dirty = true;
		refresh = true;
	}
	for (int i = 0; i < widget->childCount(); i++)
	{
		BWidget* child = widget->child(i);
		freshWidget(child, event);
	}
}
void IWindow::frameWidget(BWidget* widget)
{
	if (!widget_member(widget)->visible)
		return;
	if (!widget_member(widget)->state.shown)
		return;
	BWidgetHolderArray& children = widget_member(widget)->children;
	for (int i = children.size() - 1; i >= 0; i--)
	{
		BWidget* child = children[i];
		frameWidget(child);
	}
	if (widget_member(widget)->dirty)
	{
		widget_member(widget)->frame();
	}
}

void IWindow::drawWindow()
{
	int width = widget_member(boss)->size.width();
	int height = widget_member(boss)->size.height();
	glViewport(0, 0, width, height);

	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glOrtho(0, width, height, 0, -1, 1);

	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();

	glDisable(GL_CULL_FACE);

	BRect clip = boss->absoluteRect();

	glEnable(GL_TEXTURE_2D);
	drawWidget(boss, clip, BPoint(0, 0), clip.height());
	for (int i = 0; i < popupWidgets.size(); i++)
	{
		BWidget* widget = popupWidgets[i];
		drawPopupWidget(widget, clip, widget->absolutePos(), clip.height());
	}
	glBindTexture(GL_TEXTURE_2D, 0);
	glDisable(GL_TEXTURE_2D);
}
void IWindow::drawWidget(BWidget* widget, const BRect& clip, const BPoint& offset, int frameHeight)
{
	if (!widget_member(widget)->visible || popupWidgets.contain(widget))
		return;
	BSize size = widget->size();
	BRect ab_rect(offset, size);
	BRect view = ab_rect & clip;
	if (view.empty())
		return;

	glViewport(view.x(), frameHeight - view.bottom(), view.width(), view.height());

	BRect area(view.pos() - ab_rect.pos(), view.size());
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glOrtho(area.left() + 1, area.right(), area.bottom() - 1, area.top(), -1, 1);

	if (widget_member(widget)->tbo)
	{
		int width = widget_member(widget)->size.width();
		int height = widget_member(widget)->size.height();
		glColor4ub(255, 255, 255, widget_member(widget)->transparency);
		glBindTexture(GL_TEXTURE_2D, widget_member(widget)->tbo);
		glBegin(GL_QUADS);
		glTexCoord2f(0.0f, 1.0f);		glVertex2i(0, 0);
		glTexCoord2f(0.0f, 0.0f);		glVertex2i(0, height);
		glTexCoord2f(1.0f, 0.0f);		glVertex2i(width, height);
		glTexCoord2f(1.0f, 1.0f);		glVertex2i(width, 0);
		glEnd();
	}
	if (!widget_member(widget)->layered)
	{
		BRect ab_crect = widget->centerRect() + offset;
		BRect cview = ab_crect & clip;
		BWidgetHolderArray& children = widget_member(widget)->children;
		for (int i = 0; i < children.size(); i++)
		{
			BWidget* child = children[i];
			if (!widget_member(child)->visible)
				continue;
			if (!widget_member(child)->ignored())
				continue;
			if (widget_member(child)->floating)
				drawWidget(child, view, offset + child->pos(), frameHeight);
			else
				drawWidget(child, cview, offset + child->pos(), frameHeight);
		}
	}
}
void IWindow::drawPopupWidget(BWidget* widget, const BRect& clip, const BPoint& offset, int frameHeight)
{
	if (!widget_member(widget)->visible)
		return;
	if (!widget_member(widget)->ignored())
		return;
	BSize size = widget->size();
	BRect ab_rect(offset, size);
	BRect view = ab_rect & clip;
	if (view.empty())
		return;

	BRect ab_crect = widget->centerRect() + offset;
	BRect cview = ab_crect & clip;

	glViewport(view.x(), frameHeight - view.bottom(), view.width(), view.height());

	BRect area(view.pos() - ab_rect.pos(), view.size());
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glOrtho(area.left() + 1, area.right(), area.bottom() - 1, area.top(), -1, 1);

	if (widget_member(widget)->tbo)
	{
		glBindTexture(GL_TEXTURE_2D, widget_member(widget)->tbo);

		int width = widget_member(widget)->size.width();
		int height = widget_member(widget)->size.height();

		glBegin(GL_QUADS);
		glTexCoord2f(0.0f, 1.0f);		glVertex2i(0, 0);
		glTexCoord2f(0.0f, 0.0f);		glVertex2i(0, height);
		glTexCoord2f(1.0f, 0.0f);		glVertex2i(width, height);
		glTexCoord2f(1.0f, 1.0f);		glVertex2i(width, 0);
		glEnd();
	}
	BWidgetHolderArray& children = widget_member(widget)->children;
	for (int i = 0; i < children.size(); i++)
	{
		BWidget* child = children[i];
		if (!widget_member(child)->visible)
			continue;
		if (!widget_member(child)->ignored())
			continue;
		if (widget_member(child)->floating)
			drawWidget(child, view, offset + child->pos(), frameHeight);
		else
			drawWidget(child, cview, offset + child->pos(), frameHeight);
	}
}
void IWindow::drawCursor()
{
	if (!hoverWidget)
		return;
	BPoint globalPos = boss->globalPos();
	BPoint cursorPos = BCursor::GetPos();
	BPoint point = cursorPos - globalPos;

	BCursor* cursor = hoverWidget->cursor();
	if (cursor && cursor->type() == Cursor_None)
	{
		BPainter painter(boss);
		glBindFramebuffer(GL_FRAMEBUFFER, 0);
		if (BImage* image = cursor->image())
		{
			BPoint origin = cursor->point();
			BSize size = cursor->size();
			if (size.width() < 1)
				size.width() = image->width();
			if (size.height() < 1)
				size.height() = image->height();
			int x = point.x() - origin.x();
			int y = point.y() - origin.y();
			int w = size.width();
			int h = size.height();
			painter.setColor(255, 255, 255);
			painter.drawPoint(point);
			painter.drawImage(x, y, w, h, image);
		}
	}
}
void IWindow::drawDrag()
{
	if (!drag)
		return;
	BPoint globalPos = boss->globalPos();
	BPoint cursorPos = BCursor::GetPos();
	BPoint point = cursorPos - globalPos;

	BPainter painter(boss);

	glBindFramebuffer(GL_FRAMEBUFFER, 0);

	BPoint origin;
	BSize size(100, 100);
	int x = point.x() - origin.x();
	int y = point.y() - origin.y();
	int w = size.width();
	int h = size.height();

	BImage* image = drag->image();
	if (image)
	{
		painter.setColor(255, 255, 255);
		painter.drawPoint(point);
		painter.drawImage(x, y, w, h, image);
	}
}
void IWindow::drawTips()
{
	if (!hoverWidget || !tipsShown)
		return;

	const BString& tipsText = hoverWidget->tips();
	if (tipsText.empty())
		return;

	const BStyle* style = 0;
	if (const BStyle* realStyle = hoverWidget->realStyle())
		if (const BStyle* tipsStyle = realStyle->sub("tips"))
			style = tipsStyle;

	if (!style)
	{
		if (const BStyle* realStyle = boss->realStyle())
		{
			if (const BStyle* tipsStyle = realStyle->sub("tips"))
				style = tipsStyle;
			else
				style = realStyle;
		}
	}

	int pageWidth = 0;
	const BValue& widthValue = style->value(Value_Width);
	if (widthValue.valid())
		pageWidth = widthValue;
	if (pageWidth > boss->width())
		pageWidth = boss->width();
	tipsBlock.align = style->value(Value_Align);
	tipsBlock.reset(tipsText);
	tipsBlock.setFont(style->font());
	tipsBlock.adjust(pageWidth);
	tipsBlock.rect.setPos(tipsPoint + BPoint(23, 23));
	if (tipsBlock.rect.width() < pageWidth)
		tipsBlock.rect.setWidth(pageWidth);

	int b = style->value(Value_Border);
	int mleft = style->value(Value_Margin_Left);
	int mright = style->value(Value_Margin_Right);
	int mtop = style->value(Value_Margin_Top);
	int mbottom = style->value(Value_Margin_Bottom);

	BRect tipsArea = tipsBlock.rect;
	tipsArea.top() -= b + mtop;
	tipsArea.left() -= b + mleft;
	tipsArea.right() += b + mright;
	tipsArea.bottom() += b + mbottom;

	int xoffset = 0;
	int yoffset = 0;
	BRect rect = boss->centerRect();
	if (tipsArea.left() < rect.left())
		xoffset += rect.left() - tipsArea.left();
	if (tipsArea.top() < rect.top())
		yoffset += rect.top() - tipsArea.top();
	if (tipsArea.right() > rect.right())
		xoffset += rect.right() - tipsArea.right();
	if (tipsArea.bottom() > rect.bottom())
		yoffset += rect.bottom() - tipsArea.bottom();
	tipsArea.move(xoffset, yoffset);

	BPainter painter(boss);
	painter.setStyle(style);
	glBindFramebuffer(GL_FRAMEBUFFER, 0);

	int x = tipsArea.left();
	int y = tipsArea.top();
	int w = tipsArea.width();
	int h = tipsArea.height();

	if (painter.setColor(Color_Background))
	{
		painter.fillRect(x, y, w, h);
	}
	if (b > 0 && painter.setColor(Color_Border))
	{
		painter.fillRect(x, y + b, b, h - b - b);
		painter.fillRect(x, y, w, b);
		painter.fillRect(x + w - b, y + b, b, h - b - b);
		painter.fillRect(x, y + h - b, w, b);
	}
	if (painter.setColor(Color_Text))
	{
		tipsBlock.paint(painter);
	}
}

bool IWindow::isFullScreen()
{
	const BScreen* screen = BSystem::GetScreen();
	BSize winSize = getWindowSize();
	BSize screenSize = screen->size();
	return screenSize == winSize;
}
void IWindow::focus(BWidget* widget)
{
	if (widget && (!widget_member(widget)->state.enabled || !widget->focusable()))
		widget = 0;
	BWidget* out = 0;
	BWidget* in = 0;
	if (focusWidget != widget)
	{
		out = focusWidget;
		focusWidget = widget;
		in = focusWidget;
		refresh = true;
	}
	BEvent activeEvent(Event_Active);
	BEvent focusEvent(Event_Focus);
	if (out)
	{
		activeEvent.setValue(false);
		focusEvent.setValue(false);
		BWidget* widget = out;
		while (widget && widget != in)
		{
			if (widget->above(in))
				break;
			widget_member(widget)->state.actived = false;
			widget_member(widget)->procEvent(activeEvent);
			widget = widget_member(widget)->parent;
		}
		widget_member(out)->focused = false;
		widget_member(out)->procEvent(focusEvent);
	}
	if (in)
	{
		if (popupWidgets.contain(in) && popupWidgets.last() != in)
		{
			popupWidgets.remove(in);
			popupWidgets.append(in);
		}
		activeEvent.setValue(true);
		focusEvent.setValue(true);
		BWidget* widget = in;
		while (widget)
		{
			if (widget_member(widget)->state.actived)
				break;
			widget_member(widget)->state.actived = true;
			widget_member(widget)->procEvent(activeEvent);
			widget = widget_member(widget)->parent;
		}
		widget_member(in)->focused = true;
		widget_member(in)->procEvent(focusEvent);
	}
}
bool IWindow::popup(BWidget* widget, bool flag)
{
	if (flag)
	{
		if (!popupWidgets.contain(widget))
		{
			popupWidgets.append(widget);
		}
		widget->setFocused(true);
	}
	else
	{
		popupWidgets.remove(widget);
		for (int i = 0; i < popupWidgets.size(); i++)
		{
			BWidget* pwidget = popupWidgets[i];
			if (pwidget->under(widget))
				popupWidgets.remove(i--);
		}
		refresh = true;
		return true;
	}
	return false;
}

BWidget* IWindow::widgetUnderPoint(const BPoint& point)
{
	if (popupWidgets.size())
	{
		for (int i = popupWidgets.size() - 1; i >= 0; i--)
		{
			BWidget* widget = popupWidgets[i];
			if (!widget_member(widget)->visible)
				continue;
			if (!widget_member(widget)->ignored())
				continue;
			BWidget* result = findPopupWidget(widget, point, boss->size());
			if (result)
				return result;
			if (widget->modal())
				return 0;
		}
	}
	return findWidget(boss, point, boss->size());
}
BWidget* IWindow::findWidget(BWidget* widget, const BPoint& point, const BRect& clip)
{
	BRect rect = widget->rect();
	if (boss == widget)
		rect.setPos(0);
	BRect clipRect = rect & clip;
	if (!clipRect.contain(point))
		return 0;
	currentRect = clipRect;
	BPoint localPoint = point - rect.pos();
	BWidgetHolderArray& children = widget_member(widget)->children;
	for (int i = children.size() - 1; i >= 0; i--)
	{
		BWidget* child = children[i];
		if (!widget_member(child)->visible)
			continue;
		if (!widget_member(child)->ignored())
			continue;
		if (widget_member(child)->floating)
		{
			BWidget* ret = findWidget(child, localPoint, widget->size());
			if (ret)
				return ret;
		}
		else
		{
			const BRect& centerRect = widget_member(widget)->rectCenter;
			BWidget* ret = findWidget(child, localPoint, centerRect);
			if (ret)
				return ret;
		}
	}
	return widget;
}
BWidget* IWindow::findPopupWidget(BWidget* widget, const BPoint& point, const BRect& clip)
{
	if (!widget_member(widget)->state.enabled)
		return 0;

	BRect rect = widget->rect();

	BRect viewRect = widget->absoluteRect() & clip;
	if (!viewRect.contain(point))
		return 0;

	//BPoint localPoint = point - rect.pos();
	BWidgetHolderArray& children = widget_member(widget)->children;
	for (int i = children.size() - 1; i >= 0; i--)
	{
		BWidget* child = widget->child(i);
		if (!widget_member(child)->visible)
			continue;
		if (!widget_member(child)->ignored())
			continue;
		if (widget_member(child)->floating)
		{
			BWidget* ret = findPopupWidget(child, point, viewRect);
			if (ret)
				return ret;
		}
		else
		{
			BRect clientViewRect = widget->centerRect();
			clientViewRect += viewRect.pos();
			BWidget* ret = findPopupWidget(child, point, clientViewRect);
			if (ret)
				return ret;
		}
	}

	return widget;
}

void IWindow::procMousePress(BMouseEvent& mouseEvent)
{
	mouse_button = mouseEvent.button();
	mouse_pressed = true;
	mouse_pressed_time = mouseEvent.time();
	mouse_pressed_pos = mouseEvent.absolutePos();
	
	BWidget* widget = widgetUnderPoint(mouse_pressed_pos);
	this->focus(widget);
	if (focusWidget)
	{
		mouseEvent.setPos(mouseEvent.absolutePos() - focusWidget->absolutePos());
		widget_member(focusWidget)->procEvent(mouseEvent);
	}
}
void IWindow::procMouseRelease(BMouseEvent& mouseEvent)
{
	mouse_button = Button_None;
	mouse_pressed = false;

	if (focusWidget)
	{
		mouseEvent.setPos(mouseEvent.absolutePos() - focusWidget->absolutePos());
		widget_member(focusWidget)->procEvent(mouseEvent);
	}
	if (focusWidget && hoverWidget && focusWidget == hoverWidget && mouse_click_time > 0.0)
	{
		int distance = (mouseEvent.absolutePos() - mouse_click_pos).length();
		BReal interval = mouseEvent.time() - mouse_click_time;
		if (distance < 5 && interval < app_clickInterval)
		{
			mouseEvent.setType(Event_DoubleClick);
			mouseEvent.setPos(mouseEvent.absolutePos() - focusWidget->absolutePos());
			widget_member(focusWidget)->procEvent(mouseEvent);
			mouse_click_time = 0;
			return;
		}
	}
	if (focusWidget && mouse_pressed_time > 0.0)
	{
		int distance = (mouseEvent.absolutePos() - mouse_pressed_pos).length();
		BReal interval = mouseEvent.time() - mouse_pressed_time;
		if (distance < 5 && interval < app_clickInterval)
		{
			mouseEvent.setType(Event_MouseClick);
			mouseEvent.setPos(mouseEvent.absolutePos() - focusWidget->absolutePos());
			widget_member(focusWidget)->procEvent(mouseEvent);
			mouse_click_time = mouseEvent.time();
			mouse_click_pos = mouseEvent.absolutePos();
		}
	}
}
void IWindow::procMouseMove(BMouseEvent& mouseEvent)
{
	if (focusWidget && mouse_pressed)//block switch hovering widget
	{
		mouseEvent.setPos(mouseEvent.absolutePos() - focusWidget->absolutePos());
		widget_member(focusWidget)->procEvent(mouseEvent);
	}
	else
	{
		BPoint absolutePos = mouseEvent.absolutePos();
		BWidget* widget = widgetUnderPoint(absolutePos);
		if (hoverWidget != widget)
		{
			mouse_click_time = 0;
			BWidget* aboveWidget = widget;
			if (widget && widget_member(widget)->state.enabled)
			{
				BMouseEvent enterEvent = mouseEvent;
				enterEvent.setType(Event_MouseEnter);
				enterEvent.setPos(mouseEvent.absolutePos() - widget->absolutePos());
				widget_member(widget)->procEvent(enterEvent);
				
				BEvent hoverEvent(Event_Hover);
				hoverEvent.setValue(true);
				while (aboveWidget && !widget_member(aboveWidget)->state.hovered)
				{
					widget_member(aboveWidget)->state.hovered = true;
					widget_member(aboveWidget)->procEvent(hoverEvent);
					aboveWidget = widget_member(aboveWidget)->parent;
				}
			}
			if (hoverWidget && widget_member(hoverWidget)->state.enabled)
			{
				BMouseEvent leaveEvent = mouseEvent;
				leaveEvent.setType(Event_MouseLeave);
				leaveEvent.setPos(mouseEvent.absolutePos() - hoverWidget->absolutePos());
				widget_member(hoverWidget)->procEvent(leaveEvent);

				BEvent hoverEvent(Event_Hover);
				hoverEvent.setValue(false);
				while (hoverWidget && aboveWidget != hoverWidget)
				{
					widget_member(hoverWidget)->state.hovered = false;
					widget_member(hoverWidget)->procEvent(hoverEvent);
					hoverWidget = widget_member(hoverWidget)->parent;
				}
			}
			hoverWidget = widget;
		}
		tipsShown = false;
		tipsPoint = absolutePos;
		if (hoverWidget && widget_member(widget)->tips.empty())
			tipsTick = 0;
		else
			tipsTick = clock();
		if (hoverWidget && widget_member(hoverWidget)->state.enabled)
		{
			mouseEvent.setPos(mouseEvent.absolutePos() - hoverWidget->absolutePos());
			widget_member(hoverWidget)->procEvent(mouseEvent);
		}
	}
	BCursor* cursor = hoverWidget ? hoverWidget->cursor() : 0;
	if (hoverCursor != cursor)
	{
		hoverCursor = cursor;
		refresh = true;
	}
	if (hoverCursor && hoverCursor->type() == Cursor_None)
	{
		refresh = true;
	}
	freshCursor();
}
void IWindow::procMouseWheel(BMouseEvent& mouseEvent)
{
	if (hoverWidget && widget_member(hoverWidget)->state.enabled)
	{
		mouseEvent.setPos(mouseEvent.absolutePos() - hoverWidget->absolutePos());
		if (!widget_member(hoverWidget)->procEvent(mouseEvent))
		{
			BWidget* widget = hoverWidget;
			while (BWidget* parent = widget->parent())
			{
				mouseEvent.setPos(mouseEvent.pos() + widget->pos());
				if (widget_member(parent)->procEvent(mouseEvent))
					break;
				widget = parent;
			}
		}
	}
}
void IWindow::procDragMove(BDragEvent& dragEvent)
{
	drag = dragEvent.drag();

	BPoint absolutePos = dragEvent.absolutePos();
	if (!boss->absoluteRect().contain(absolutePos))
		drag = 0;

	BWidget* widget = widgetUnderPoint(absolutePos);
	if (hoverWidget != widget)
	{
		mouse_click_time = 0;
		if (hoverWidget)
		{
			dragEvent.setCursor(0);
			dragEvent.setType(Event_DragLeave);
			dragEvent.setPos(dragEvent.absolutePos() - hoverWidget->absolutePos());
			widget_member(hoverWidget)->procEvent(dragEvent);
		}
		if (widget && widget_member(widget)->state.enabled)
		{
			dragEvent.setCursor(0);
			dragEvent.setType(Event_DragEnter);
			dragEvent.setPos(dragEvent.absolutePos() - widget->absolutePos());
			widget_member(widget)->procEvent(dragEvent);
		}
		hoverWidget = widget;
	}
	if (hoverWidget && widget_member(hoverWidget)->state.enabled)
	{
		dragEvent.setType(Event_DragMove);
		dragEvent.setPos(dragEvent.absolutePos() - hoverWidget->absolutePos());
		widget_member(hoverWidget)->procEvent(dragEvent);
	}
	this->refresh = true;
}
void IWindow::procDragDrop(BDragEvent& dragEvent)
{
	if (hoverWidget)
	{
		widget_member(hoverWidget)->procEvent(dragEvent);
	}
	drag = 0;
}

